﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;

namespace CommonUtil
{
    public class RoleRightUser
    {
        /// <summary>
        /// 调用存储过程YZ_RoleRightUserQuery，返回不同类型的DataTable
        /// </summary>
        /// <param name="dtType">类型</param>
        /// <param name="roleId">角色ID</param>
        /// <param name="userId">用户ID</param>
        /// <returns></returns>
        public static DataTable GetSimpleDataTable(DataTableType dtType ,string roleId, string userId)
        {
            string ProcName = "YZ_RoleRightUserQuery";
            string result = "";
            string resultCode = "";
            DataSet ds = null;
            DataTable dt = null;   //执行存储过程后的DataTable
            string[] paramString = new string[3];

            try
            {
                paramString[0] = string.Format("@Type~VarChar~Input~50~{0}", dtType.ToString());
                paramString[1] = string.Format("@RoleId~VarChar~Input~36~{0}", roleId == "" ? "" : roleId);
                paramString[2] = string.Format("@UserId~VarChar~Input~11~{0}", userId == "" ? "" : userId);

                ds = SqlHelper.ExecuteDataSet(ProcName, SqlHelper.QucikCreatePrams(paramString), new string[] { "DataSetTable", "ResultDt" });
                dt = ds.Tables["DataSetTable"];
                result = ds.Tables["ResultDt"].Rows[0]["RESULT"].ToString();
                resultCode = result.Split('~')[0];
                if (resultCode != "0")
                {
                    dt = null;
                }
            }
            catch (Exception ex)
            {
                string exStr = string.Format("执行存储过程[" + ProcName + "]结果：" + result + "\r\n{0}\r\n{1}\r\n{2}", ex.Message, ex.Source, ex.StackTrace);
                Logger.DefaultLog(exStr, LogType.Error);
                throw ex;
            }
            return dt;
        }
        /// <summary>
        /// 调用存储过程YZ_RoleRightUserFunc，执行不同类型操作
        /// </summary>
        /// <param name="funcType">类型</param>
        /// <param name="roleId">角色ID</param>
        /// <param name="roleName">角色名</param>
        /// <param name="userId">用户ID</param>
        /// <param name="menuFuncList">菜单功能列表</param>
        /// <param name="specRightList">特殊权限列表</param>
        /// <returns></returns>
        public static string[] RoleRightUserOpear(FuncType funcType, string roleId, string roleName, string userId, string menuFuncList, string specRightList)
        {
            string ProcName = "YZ_RoleRightUserFunc";
            string[] resultStr = new string[2] { "", "" };
            string[] paramString = new string[7];
            try
            {
                paramString[0] = string.Format("@Type~VarChar~Input~50~{0}", funcType.ToString());
                paramString[1] = string.Format("@RoleId~VarChar~Input~36~{0}", roleId == "" ? "" : roleId);
                paramString[2] = string.Format("@RoleName~VarChar~Input~20~{0}", roleName == "" ? "" : roleName);
                paramString[3] = string.Format("@UserId~VarChar~Input~11~{0}", userId == "" ? "" : userId);
                paramString[4] = string.Format("@MenuFuncList~VarChar~Input~255~{0}", menuFuncList == "" ? "" : menuFuncList);
                paramString[5] = string.Format("@SpecRightList~VarChar~Input~255~{0}", specRightList == "" ? "" : specRightList);
                paramString[6] = "@ExecResult~VarChar~Output~255~0";

                Hashtable ht = SqlHelper.ExecuteProc(ProcName, SqlHelper.QucikCreatePrams(paramString));
                if (ht != null)
                {
                    resultStr = ht["@ExecResult"].ToString().Split('~');
                }
            }
            catch (Exception ex)
            {
                string exStr = string.Format("执行存储过程[" + ProcName + "]结果：" + resultStr + "\r\n{0}\r\n{1}\r\n{2}", ex.Message, ex.Source, ex.StackTrace);
                Logger.DefaultLog(exStr, LogType.Error);
                throw ex;
            }
            return resultStr;
        }
    }

    #region 枚举，存储过程入参类型
    /// <summary>
    /// [YZ_RoleRightUserQuery]
    /// </summary>
    public enum DataTableType
    {
        	GetRoleList,  //获取可用角色            
            GetUserList, //获取所属角色下的用户，入参为@RoleId
            GetUserNullRole,	//获取没有分配角色的用户	            
        	
            GetAllMenuFunc,	//获取有效的菜单功能
            GetAllSpecRight,	//获取有效的特殊权限	            

            GetRoleMenuFunc,	//获取角色的菜单功能，入参为@RoleId
            GetRoleSpecRight,	//获取角色的特殊权限，入参为@RoleId

            GetUserMenuFunc,	//获取用户的菜单功能，入参为@RoleId，@UserId
            GetUserSpecRight	//获取用户的特殊权限，入参为@RoleId，@UserId	            
    }
    /// <summary>
    /// [YZ_RoleRightUserFunc]
    /// </summary>
    public enum FuncType
    {
        AddRole,       //添加角色，入参为@RoleId，@RoleName
        DelRole,	    //删除角色，入参为@RoleId（需要判断是否存在属于该角色的用户）
        AddUserRoleId, 	//添加用户角色内码，入参为@RoleId，@UserId
        DelUserRoleId,	    //删除用户角色内码，入参为@UserId，置RoleId,HideenFuncList,HieednSpecList为空。
        UpdateRoleRight,	    //更新角色的菜单权限，入参为@RoleId，@MenuFuncList，@SpecRightList
        UpdateUserRight,	    //更新用户的权限，入参为@UserId，@MenuFuncList，@SpecRightList
        AddSpecRight,	    //增加特殊权限（预留）
        DelSpecRight	    //删除特殊权限（预留）
    }
    #endregion 
}
